package com.kongfuzi.student.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.kongfuzi.student.R;
import com.kongfuzi.student.app.GasApplication;
import com.kongfuzi.student.bean.MessageEvent;
import com.kongfuzi.student.support.utils.ToastUtil;
import com.kongfuzi.student.ui.gaspay.GasPayResultActivity;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;

import de.greenrobot.event.EventBus;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxpay_result);
        
    	api = GasApplication.getWXAPI();

        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if(resp.errCode == 0) { // 支付成功
				startActivity(GasPayResultActivity.startSuccessInstance());
				EventBus.getDefault().post(new MessageEvent(MessageEvent.MessageType
						.ACTIVITY_FINISH));
			} else if (resp.errCode == -1) { // 支付失败
				startActivity(GasPayResultActivity.startFailedInstance());
				EventBus.getDefault().post(new MessageEvent(MessageEvent.MessageType.ACTIVITY_FINISH));
			} else if (resp.errCode == -2) { //用户取消
				ToastUtil.showToast(this,"支付取消");
			}
			finish();
		}
	}
}